package BlogMybatis.blog.controller;


import BlogMybatis.blog.pojo.Blog;
import BlogMybatis.blog.pojo.Type;
import BlogMybatis.blog.service.BlogService;
import BlogMybatis.blog.service.TypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class TypeShowController {
    @Autowired
    private TypeService typeService;
    @Autowired
    private BlogService blogService;

    @GetMapping("/types/{id}")
    public String types(@PathVariable("id") Long id, @RequestParam(required = false,defaultValue = "1") int pageNum,
                        Model model){
        PageHelper.startPage(pageNum,1000);
        List<Type> types = typeService.getIndexType();
        //点击首页分类时，默认传一个-1
        if(id == -1){
            id = types.get(0).getId();
        }
        List<Blog> blogs = blogService.getByTypeId(id);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogs);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("types",types);
        model.addAttribute("activeTypeId",id);
        return "types";
    }
}
